<?php
/*
Plugin Name: WP Coupon Poster
Plugin URI: http://www.WPCouponPoster.com
Description: Increase clickthroughs & comissions on your affiliate links using coupons.
Author: Madhu
Version: 1.0
Author URI: http://www.WPCouponPoster.com

*/

// Stop direct call
if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
    die('You are not allowed to call this page directly (wishwdgt).');
}

if (!class_exists('wvcr')) {
    class wvcr {
	
		var $acp; // Admin Control Panel
		var $db; // Database Manipulation
		var $installer; // Plugin Installer
		
		var $options; // Options Page

		var $coupon_mb; // Coupon MetaBox
		
        var $minimum_WP = '3.0';

        function wvcr() {
            // Stop the plugin if we missed the requirements
            if (!$this->required_version()) return;

            // Register_taxonomy must be used during the init and before loading any dependencies
			add_action('init', array(&$this, 'register_custom_things'));
            add_action('init', array(&$this, 'load_dependencies'), 1);
			add_shortcode('coupon', array(&$this, 'wv_coupon_shortcode'));

            // Get some constants first
            $this->define_constant();
            $this->define_tables();
            $this->plugin_name = plugin_basename(__FILE__);

            // Init options & tables during activation & deregister init option
            register_activation_hook($this->plugin_name, array(&$this, 'activate'));
            register_deactivation_hook($this->plugin_name, array(&$this, 'deactivate'));

            // Register a uninstall hook to remove all tables & option automatic
            register_uninstall_hook($this->plugin_name, array(&$this, 'uninstall'));
        }

        function register_custom_things() {
			// Register custom post type: Coupon
            $labels = array('name' => __('Coupons', "wv"), 'singular_name' => __('Coupon', "wv"),
                            'add_new' => __('Add New', "wv"),
                            'add_new_item' => __('Add New Coupon', "wv"),
                            'edit_item' => __('Edit Coupon', "wv"),
                            'new_item' => __('New Coupon', "wv"),
                            'view_item' => __('View Coupon', "wv"),
                            'search_items' => __('Search Coupon', "wv"),
                            'not_found' => __('No Coupon found', "wv"),
                            'not_found_in_trash' => __('No Coupon found in Trash', "wv"),
                            'parent_item_colon' => '', 'menu_name' => __('Coupons', "wv"));

            $args = array(
                'labels' => $labels,
                'public' => false,
                'publicly_queryable' => false,
                'show_ui' => true,
                'show_in_menu' => true,
                'query_var' => false,
                'rewrite' => false,
                'capability_type' => 'post',
                'has_archive' => false,
                'hierarchical' => false,
                'menu_position' => null,
                'supports' => array('title'));

            register_post_type('coupon', $args);

			$this->coupon_mb = new WPAlchemy_MetaBox(array(
				'id' => '_wv_coupon',
				'title' => 'Coupon Reveal',
				'types' => array('coupon'),
				'template' => dirname(__FILE__) . '/admin/views/metabox.php'
			));

			
        }

		function wv_coupon_shortcode($atts, $content=null, $code="")
		{
			global $wvcr;
			
			$id = $atts['id'];

			if ( ! $id ) {
				return '';
			}
			
			$meta = $wvcr->coupon_mb->the_meta($id);
			$coupon = &get_post($id);
			
			$border_color = get_post_meta($id, 'border_color', 1) == '' ? '004080' : get_post_meta($id, 'border_color', 1);
			$bg_color = get_post_meta($id, 'bg_color', 1) == '' ? 'cce3ff' : get_post_meta($id, 'bg_color', 1);
			$text_color = get_post_meta($id, 'text_color', 1) == '' ? 'CC0066' : get_post_meta($id, 'text_color', 1);
			$code_color = get_post_meta($id, 'code_color', 1) == '' ? '0000ff' : get_post_meta($id, 'code_color', 1);
			$code_bg_color = get_post_meta($id, 'code_bg_color', 1) == '' ? 'ffff00' : get_post_meta($id, 'code_bg_color', 1);
			$width = get_post_meta($id, 'width', 1) == '' ? 'auto' : get_post_meta($id, 'width', 1);
	
			
			$html  ='<div class="coupon-wrapper">';
			$html .=	'<div class="couponcodes" style="width: '.$width.'; background-color: #'.$bg_color.'; border-color: #'.$border_color.';">';
			$html .=		'<p style="color: #'.$text_color.';">';
			$html .=			'<span class="coupon-title">'.$coupon->post_title.'</span>';
			$html .=			'<span class="coupon-saved">'.__('You Save: ').$wvcr->coupon_mb->get_the_value('saved').'</span>';
			$html .=			'<br/>';
			$html .=			'<span class="coupon-desc">'.strip_tags($coupon->post_content).'</span>';
			$html .=			'<a target="_blank" href="'.$wvcr->coupon_mb->get_the_value('couponurl').'" class="coupon-code" style="color: #'.$code_color.'; background-color: #'.$code_bg_color.';">'.strtoupper($wvcr->coupon_mb->get_the_value('couponcode')).'</a>';
			$html .= 			'<br class="clear"/>';
			$html .=		'</p>';
			$html .=	'</div>';
			$html .=	'<p class="coupon-note">'.__('* click to copy code and open website. ').__('Coupon Expires: ').$wvcr->coupon_mb->get_the_value('enddate').'</p>';
			$html .='</div>';

			return $html;
		}

        function required_version() {
            global $wp_version;

            // Check for WP version installation
            $wp_ok = version_compare($wp_version, $this->minimum_WP, '>=');
            if (($wp_ok == FALSE)) {
                add_action('admin_notices', create_function('', 'global $wvcr; printf (\'<div id="message" class="error"><p><strong>\' . __(\'Sorry, WordPress Coupon Reveal works only under WordPress %s or higher\', "wvcr" ) . \'</strong></p></div>\', $wvcr->minimum_WP );'));
                return false;
            }

            return true;
        }

        function render($template, $data = array(), $admin = true) {

            extract($data);

            if (!$template) {
                return false;
            }
            if (!$admin) {
                include_once (dirname(__FILE__) . '/views/' . $template . '.php');
            } else {
                include_once (dirname(__FILE__) . '/admin/views/' . $template . '.php');
            }
        }

        function define_constant() {
            define('WVVERSION', $this->version);

            // define URL
            define('WVFOLDER', plugin_basename(dirname(__FILE__)));

            define('WV_ABSPATH', trailingslashit(str_replace("\\", "/", WP_PLUGIN_DIR . '/' . plugin_basename(dirname(__FILE__)))));
            define('WV_URLPATH', trailingslashit(WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__))));
        }

        function define_tables() {
            global $wpdb;
        }

        function load_dependencies() {

			require_once (dirname(__FILE__) . '/lib/wvdb.php');
			require_once (dirname(__FILE__) . '/lib/MetaBox.php');

            if (is_admin()) {
                require_once (dirname(__FILE__) . '/admin/admin.php');
                require_once (dirname(__FILE__) . '/admin/functions.php');
                require_once (dirname(__FILE__) . '/admin/install.php');
            } else {
                require_once (dirname(__FILE__) . '/functions.php');
            }
        }

        function activate() {
            flush_rewrite_rules();

        }

        function deactivate() {
            flush_rewrite_rules();
        }

        function install() {
            // wv_install();
        }

        function uninstall() {
            // wv_uninstall();
        }

    }

    // Start the holy plugin
    global $wvcr;
    $wvcr = new wvcr();
}